#!/usr/local/jruby/bin/jruby
require 'java'

include_class "javax.swing.JFrame"
include_class "java.awt.BorderLayout"
include_class "com.sun.opengl.util.Animator"
include_class "javax.media.opengl.GL"
include_class "javax.media.opengl.GLCanvas"
include_class "javax.media.opengl.GLCapabilities"
include_class "javax.media.opengl.GLAutoDrawable"
include_class "javax.media.opengl.GLDrawableFactory"
include_class "javax.media.opengl.GLEventListener"
include_class "com.sun.opengl.util.GLUT"



$gl_event_listener = GLEventListener.new
class << $gl_event_listener
  def init(drawable)
     gl = drawable.getGL
    glut = GLUT.new

    gl.glShadeModel(GL::GL_SMOOTH);
    
    gl.glEnable(GL::GL_LIGHTING)
    gl.glEnable(GL::GL_LIGHT0)
    #gl.glEnable(GL::GL_DEPTH_TEST)
    #gl.glDepthFunc(GL::GL_LESS)
    gl.glEnable(GL::GL_CULL_FACE)
  end
  
  def reshape(drawable, x, y, width, height)
  
  end

  def displayChanged(drawable, modeChanged, deviceChanged)
    
  end
  def display(drawable)
      gl = drawable.getGL();
      glut = GLUT.new
      gl.glRotated(0.15, 0, 1, 0);  
      gl.glRotated(0.15, 1, 1, 0);  
      gl.glClear(GL::GL_COLOR_BUFFER_BIT);
      gl.glColor4d(1,0,0,0);
      
      mat_specular = glFloatArray [1.0, 1.0, 1.0, 1.0]
      mat_shininess = glFloatArray [50.0]
      light_position = glFloatArray [1.0, 1.0, 1.0, 0.0]

      gl.glMaterialfv(GL::GL_FRONT, GL::GL_SPECULAR, mat_specular);  
      gl.glMaterialfv(GL::GL_FRONT, GL::GL_SHININESS, mat_shininess);
      gl.glLightfv(GL::GL_LIGHT0, GL::GL_POSITION, light_position);
      
      
      glut.glutSolidSphere 0.7, 20, 20
      gl.glFlush();
  end
  
  def glFloatArray(array)
    ret = java.nio.FloatBuffer.allocate(array.length)
    array.each do |n|
      ret.put(n)
    end
    ret
  end
  
end

$hellowrld = JFrame.new
class << $hellowrld
    
    def init_win
      setLayout(BorderLayout.new);

      setSize(400, 400);
      setLocation(40, 40);
      setupJOGL()
      setVisible(true);
    end

    def setupJOGL()
        caps = GLCapabilities.new
        caps.setDoubleBuffered(true);
        caps.setHardwareAccelerated(true);

        canvas = GLCanvas.new(caps);
        canvas.addGLEventListener($gl_event_listener);

        add(canvas, BorderLayout::CENTER);

        anim = Animator.new(canvas);
        anim.start();
    end
    
end

$hellowrld.init_win

